package com.shiwaixiangcun.customer.ui.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jaeger.recyclerviewdivider.RecyclerViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shiwaixiangcun.customer.Common;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.adapter.AdapterMine;
import com.shiwaixiangcun.customer.base.BaseTwoFragment;
import com.shiwaixiangcun.customer.entity.MineEntity;
import com.shiwaixiangcun.customer.event.SimpleEvent;
import com.shiwaixiangcun.customer.module.intelligent.IntelligentDeviceActivity;
import com.shiwaixiangcun.customer.module.mall.AfterServiceActivity;
import com.shiwaixiangcun.customer.module.mall.ManageAddressActivity;
import com.shiwaixiangcun.customer.module.mall.OrderActivity;
import com.shiwaixiangcun.customer.module.property.MyPropertyActivity;
import com.shiwaixiangcun.customer.module.ticket.MyTicketsActivity;
import com.shiwaixiangcun.customer.ui.activity.AboutActivity;
import com.shiwaixiangcun.customer.ui.activity.FamilyActivity;
import com.shiwaixiangcun.customer.ui.activity.InformationActivity;
import com.shiwaixiangcun.customer.ui.activity.LoginActivity;
import com.shiwaixiangcun.customer.ui.dialog.DialogLoginOut;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.SharePreference;
import com.shiwaixiangcun.customer.utils.Utils;
import com.shiwaixiangcun.customer.widget.RoundImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.Unbinder;


/**
 * @author Administrator
 */
public class FragmentMine extends BaseTwoFragment {
    @BindView(R.id.rv_mine)
    RecyclerView mRvMine;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    Unbinder unbinder;
    View mHeaderView = null;
    private TextView mTvUserName;
    private RoundImageView mIvAvatar;
    private AdapterMine mAdapterMine = null;
    private ArrayList<MineEntity> mMineEntities;

    private Intent mIntent;

    public FragmentMine() {

    }


    public static FragmentMine newInstance() {
        FragmentMine fragment = new FragmentMine();
        return fragment;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateUI(SimpleEvent simpleEvent) {
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.transparentStatusBar()
                .init();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_mine;
    }


    @Override
    protected void initView() {
        super.initView();

        mMineEntities = new ArrayList<>();
        mAdapterMine = new AdapterMine(mMineEntities);
        if (mHeaderView == null) {
            mHeaderView = LayoutInflater.from(mContext).inflate(R.layout.layout_mine_header, null, false);
            mIvAvatar = mHeaderView.findViewById(R.id.iv_mine);
            mTvUserName = mHeaderView.findViewById(R.id.tv_user_name);
            mAdapterMine.addHeaderView(mHeaderView);
        } else {
            mIvAvatar = mHeaderView.findViewById(R.id.iv_mine);
            mTvUserName = mHeaderView.findViewById(R.id.tv_user_name);
            mAdapterMine.addHeaderView(mHeaderView);
        }
        mRvMine.setLayoutManager(new LinearLayoutManager(mContext));
        mRvMine.setAdapter(mAdapterMine);
        RecyclerViewDivider divider = new RecyclerViewDivider.Builder(this.getActivity())
                .setOrientation(RecyclerViewDivider.VERTICAL)
                .setStyle(RecyclerViewDivider.Style.END)
                .setStartSkipCount(1)
                .setMarginLeft(61)
                .setMarginRight(20)
                .setSize(0.5f)
                .setColorRes(R.color.color_divider_0_1)
                .build();
        mRvMine.addItemDecoration(divider);

        // 点击头像区域进入个人信息页面
        mHeaderView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String isLogin = SharePreference.getStringSpParams(mContext, Common.ISORNOLOGIN, Common.SIORNOLOGIN);

                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, InformationActivity.class);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    mIntent.putExtra("mineLogin", "mineLogin");
                    startActivityForResult(mIntent, 1020);
                }
            }
        });
        mAdapterMine.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MineEntity data = (MineEntity) adapter.getData().get(position);
                clickItem(data);
            }
        });

    }


    /**
     * 处理点击事件
     *
     * @param data 点击Item的子项
     */
    private void clickItem(MineEntity data) {
        //判断是否登录
        String isLogin = SharePreference.getStringSpParams(mContext, Common.ISORNOLOGIN, Common.SIORNOLOGIN);
        switch (data.flag) {
            case "ORDER":
                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, OrderActivity.class);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    startActivity(mIntent);
                }
                break;
            case "AFTER_SALE":
                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, AfterServiceActivity.class);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    startActivity(mIntent);
                }
                break;
            case "ADDRESS":
                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, ManageAddressActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("clickable", false);
                    mIntent.putExtras(bundle);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    startActivity(mIntent);
                }
                break;
            case "FAMILY":

                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, FamilyActivity.class);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    startActivity(mIntent);

                }
                break;

            case "NUMBER":
                showDialog(data.rightInfo);
                break;

            case "ABOUT":

                mIntent = new Intent(mContext, AboutActivity.class);
                startActivity(mIntent);
                break;
            case "TICKET":
                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, MyTicketsActivity.class);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    startActivity(mIntent);
                }
                break;
            case "INTELLIGENT":
                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, IntelligentDeviceActivity.class);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    startActivity(mIntent);
                }
                break;

            case "PROPERTY":
                if (Utils.isNotEmpty(isLogin)) {
                    mIntent = new Intent(mContext, MyPropertyActivity.class);
                    startActivity(mIntent);
                } else {
                    mIntent = new Intent(mContext, LoginActivity.class);
                    startActivity(mIntent);
                }

            default:
                break;


        }
    }

    /**
     * 显示拨打电话 对话框
     *
     * @param phone 电话号码
     */

    private void showDialog(final String phone) {
        final DialogLoginOut dialogLoginOut = new DialogLoginOut(mContext, R.layout.item_dialog_call_phone);
        dialogLoginOut.setTitle("是否要拨打此电话？");
        dialogLoginOut.setMessage(phone);
        dialogLoginOut.setYesOnclickListener("是", new DialogLoginOut.OnPositiveOnclickListener() {
            @Override
            public void onYesClick() {
                dialogLoginOut.dismiss();
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(intent);
            }
        });

        dialogLoginOut.setNoOnclickListener("否", new DialogLoginOut.OnPassiveOnclickListener() {
            @Override
            public void onNoClick() {

                dialogLoginOut.dismiss();
            }
        });
        dialogLoginOut.show();
    }

    /**
     * 初始化Data 数据
     */
    @Override
    protected void initData() {
        MineEntity property = new MineEntity(R.drawable.icon_mine_property, 9, "PROPERTY", "我的房产", "");
        MineEntity order = new MineEntity(R.drawable.icon_mine_order, 1, "ORDER", "我的订单", "");
        MineEntity ticket = new MineEntity(R.drawable.icon_mine_ticket, 2, "TICKET", "我的车票", "");
        MineEntity afterSale = new MineEntity(R.drawable.icon_mine_after, 3, "AFTER_SALE", "售后服务", "");
        MineEntity address = new MineEntity(R.drawable.icon_mine_address, 4, "ADDRESS", "收货地址", "");
        MineEntity family = new MineEntity(R.drawable.icon_mine_family, 5, "FAMILY", "我的家人", "");
        MineEntity intelligentDevice = new MineEntity(R.drawable.icon_mine_device, 6, "INTELLIGENT", "智能设备", "");
        MineEntity about = new MineEntity(R.drawable.icon_mine_about, 8, "ABOUT", "关于世外生活", "");
        mMineEntities.add(order);
        mMineEntities.add(ticket);
        mMineEntities.add(afterSale);
        mMineEntities.add(property);
        mMineEntities.add(address);
        mMineEntities.add(family);
        mMineEntities.add(intelligentDevice);
        mMineEntities.add(about);
        mAdapterMine.notifyDataSetChanged();


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        String isLogin = SharePreference.getStringSpParams(mContext, Common.ISORNOLOGIN, Common.SIORNOLOGIN);
        if (Utils.isNotEmpty(isLogin)) {
            String strUserAvatar = (String) AppSharePreferenceMgr.get(mContext, GlobalConfig.USER_AVATAR, "");
            String stringSpParams = SharePreference.getStringSpParams(mContext, Common.ISIMAGEHEAD, Common.SIIMAGEHEAD);
            String username = SharePreference.getStringSpParams(mContext, Common.ISUSERNAME, Common.SIUSERNAME);
            mTvUserName.setText(username);
            if (Utils.isNotEmpty(strUserAvatar)) {
                Glide.with(this).load(strUserAvatar)
                        .error(R.mipmap.defalt_image)
                        .into(mIvAvatar);
            } else {
                mIvAvatar.setImageResource(R.mipmap.defalt_image);
            }

        } else {
            mTvUserName.setText("立即登录");
            mIvAvatar.setImageResource(R.mipmap.defalt_image);
        }
    }

}